package org.gruposp2p.dnie.server.rest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gruposp2p.dnie.model.DNIeDocumentToSign;
import org.gruposp2p.dnie.model.DNIeSignedDocument;
import org.gruposp2p.dnie.model.DNIeUser;
import org.gruposp2p.dnie.service.DNIeSignedDocumentDAO;
import org.gruposp2p.dnie.service.DNIeUserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
/**
 *
 * @author jj
 */
@Path(value = "/private/userSignedDocuments")
//@Path(value = "/userSignedDocuments")
@Component
@Scope("request")
public class UserSignedDocumentsResource {

    private static Logger logger =
            LoggerFactory.getLogger(UserSignedDocumentsResource.class);

    @Autowired
    private DNIeSignedDocumentDAO signedDocumentDao;
    @Autowired
    private DNIeUserDAO userDao;

    @Context HttpServletRequest request;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public Response get(
            @QueryParam("firstResult") @DefaultValue("0") int firstResult,
            @QueryParam("numberResults") @DefaultValue("15") int numberResults) {
        List<DNIeSignedDocument> signedDocs = null;
        List<DNIeDocumentToSign> docomentsToSign = null;
        DNIeUser dnieUserId = validateUser();
        try {
            signedDocs = signedDocumentDao.findUserList(firstResult, numberResults, dnieUserId);
            if (signedDocs != null) {
                docomentsToSign = new ArrayList<DNIeDocumentToSign>();
                for(DNIeSignedDocument signedDocument : signedDocs){
                    docomentsToSign.add(signedDocument.getDnieDocumentToSignId());
                }
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
    	}
        GenericEntity<Collection<DNIeDocumentToSign>> ge =
                new GenericEntity<Collection<DNIeDocumentToSign>>(docomentsToSign) {};
        return Response.ok().entity(ge).build();
    }

    
    private DNIeUser validateUser () {
        SecurityContext securityContext =
            (SecurityContext) SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        logger.debug("authentication.getPrincipal().getClass(): " + authentication.getPrincipal().getClass());
        DNIeUser user = (DNIeUser)authentication.getPrincipal();
        if (userDao.findByNif(user.getNif()) == null) {
            userDao.save(user);
        }
        logger.debug("user.getNif(): " + user.getNif());
        return user;
    }

    /*
    private DNIeUser validateUser1 () {
        DNIeUser user = userDao.findById(Integer.valueOf(1));
        return user;
    }*/

}
